/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.corespring.restaurant.web.controller;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.xml.JRPenFactory.Right;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.corespring.restaurant.model.Categoria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.corespring.restaurant.model.Plato;
import org.corespring.restaurant.model.Tipo;
import org.corespring.restaurant.model.Usuario;
import org.corespring.restaurant.service.PlatoManager;
import org.corespring.restaurant.service.ReporteManager;
import org.corespring.restaurant.service.UsuarioManager;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

/**
 *
 * @author Aldo
 */
@Controller
public class PlatoController {

    PlatoManager platoManager;
    UsuarioManager usuarioManager;
    ReporteManager reporterManager;

    @Autowired
    public PlatoController(PlatoManager oPlatoManager, UsuarioManager oUsuarioManager, ReporteManager oReporteManager) {
        this.platoManager = oPlatoManager;
        this.usuarioManager = oUsuarioManager;
        this.reporterManager = oReporteManager;
    }

    @RequestMapping("/adm_platos1.htm")
    public void adm_platos1(Model model, @ModelAttribute("Plato") Plato plato) {
        List<Plato> platos = platoManager.obtenerListaPlatos();
        model.addAttribute("platos", platos);
        model.addAttribute("plato", plato);
    }

    @RequestMapping("/adm_platos.htm")
    public void adm_platos2(Model model, @ModelAttribute("Plato") Plato plato) {
        List<Plato> platos = platoManager.obtenerListaPlatos(plato);
        model.addAttribute("platos", platos);
        model.addAttribute("plato", plato);
    }

    @RequestMapping(value = "/adm_registroPlato.htm", method = RequestMethod.GET)
    public 
    @ModelAttribute("plato")
    Plato obtenerPlato(
            @RequestParam(value = "idplato", required = false) String idPlato, HttpServletRequest request) {
        if (idPlato == null) {
            return new Plato();
        }
        if (!(idPlato.isEmpty() || idPlato.equals(""))) {
            Plato plato = platoManager.obtenerPlatoById(idPlato);
            if (plato == null) {
                return new Plato();
            }
            return plato;
        }
        return new Plato();
    }

    @RequestMapping(value = "/adm_registroPlato.htm", method = RequestMethod.POST)
    public String guardarPlato(@RequestParam(value = "cboEstado", required = false) char estado,
            @ModelAttribute("plato") Plato plato,
            BindingResult result, SessionStatus status) {
        Date fecha = new Date();
        String hora = fecha.getHours() + ":" + fecha.getMinutes() + ":" + fecha.getSeconds();

        Usuario usu = usuarioManager.obtenerUsuarioById("admin");

        plato.setEreg(estado);
        plato.setFreg(fecha);
        plato.setHreg(hora);
        plato.setUsuario(usu);

        platoManager.guardarPlato(plato);
        return "redirect:adm_platos.htm";
    }

    @ModelAttribute("categoria")
    public List<Categoria> listarCategorias() {

        List<Categoria> categorias = platoManager.obtenerCategorias();
        return categorias;
    }

    @ModelAttribute("tipo")
    public List<Tipo> listarTipos() {

        List<Tipo> tipos = platoManager.obtenerTipos();
        return tipos;
    }

    @RequestMapping("/adm_eliminarPlato.htm")
    public String eliminarPlato(
            @RequestParam(value = "idplato", required = false) String idplato) {
        platoManager.eliminarPlato(idplato);
        return "redirect:adm_platos.htm";
    }

    @RequestMapping("/exportarListaPlatosReporte.htm")
    public void exportarListaPlatosReporte(HttpServletResponse response) throws Exception {
        OutputStream out = response.getOutputStream();
        HSSFWorkbook dataWorkbook = null;

        try {
            dataWorkbook = reporterManager.obtenerListaPlatosReporte();
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment; filename=\"" + "PlatosReporte.xls" + "\"");

        } catch (Exception e) {
            System.out.println("El siguiente error ocurrio: " + e.getMessage());
        } finally {
            dataWorkbook.write(out);
            out.close();
        }
    }

    @RequestMapping("/platos.htm")
    public void platos(Model model, @ModelAttribute("Plato") Plato plato) {
        List<Plato> platos = new ArrayList<Plato>();
        int variable = 0;
        int nTot = platoManager.obtenerListaPlatos().size();
        String codigo = "";
        for (int i = 0; i <= nTot; i++) {
            variable = i + 1;//(int) (1 + Math.random() * 10);

            if (variable > 9 && variable <= nTot) {
                codigo = "P" + variable;
            } else if (variable < 10) {
                codigo = "P0" + variable;
            }
            if (variable > nTot) {
                break;
            }

            Plato oplato = platoManager.obtenerPlatoById(codigo);
            if (oplato.getIdplato() != null && !oplato.getIdplato().isEmpty()) {
                platos.add(oplato);
            }
            //System.out.println("Variable -XXXXXXs " + variable);
            //if (platos.size() == 4) {
            //  break;
            //}
        }
        model.addAttribute("platos", platos);
        model.addAttribute("plato", plato);
        //return "redirect:html/adm_usuarios.jsp";
    }
}
